#define channumber 6 //How many channels have your radio???
#define filter 10 //Glitch Filter
int channel[channumber]; //readed Channel values
int lastReadChannel[channumber]; //Last  values readed
int conta = 0; //couter


void setup() {
	Serial.begin(9600); //Serial Begin
	pinMode(4, INPUT); //Pin 4 as input
	pinMode(13, OUTPUT); // Led pin 13
}

void loop() {

	if (pulseIn(4, HIGH) > 3000) //If pulse > 3000 seconds, continues
	{
		for (int i = 0; i <= channumber - 1; i++) //Read the pulses of the channels
		{
			channel[i] = pulseIn(4, HIGH);
		}
		for (int i = 0; i <= channumber - 1; i++) //Average the pulses
		{
			if ((channel[i] > 2000) || (channel[i] < 100))//If channel > max range, chage the value to the last pulse
			{
				channel[i] = lastReadChannel[i];
			} else {
				channel[i] = (lastReadChannel[i] + channel[i]) / 2; //Average the last pulse eith the current pulse
				conta++; //increment counter
			}
		}

	}
	if (conta > filter)//If counter is > than filter, then prints values
	{
		for (int i = 0; i <= channumber - 1; i++) //Cycle to print values
		{
			Serial.print("CH"); //Channel
			Serial.print(i + 1); //Channel number
			Serial.print(": "); // que te importa
			Serial.println(channel[i]);
			lastReadChannel[i] = channel[i];
		}
		if (channel[4] > 1000) //If channel 5 is > than 500 turn on the led
		{
			digitalWrite(13, HIGH);
		} else {
			digitalWrite(13, LOW);//If not turn it off
		}
		delay(400); //Delay
		conta = 0;// Restart couter.
	}
}
